import axios from 'axios'
import { LocalStorage } from 'quasar'
const Url = process.env.API
const timeOut = 100000

let jsonFunction = config => {
  const token = LocalStorage.get.item('admin-token')
  config.data = JSON.stringify(config.data)
  config.headers = {
    'Content-Type': 'application/json'
  }
  if (token) {
    config.headers.common = {'Authorization': 'Bearer' + ' ' + token}
  }
  return config
}

let fileFunction = config => {
  const token = LocalStorage.get.item('admin-token')
  config.headers = {
    'Content-Type': 'multipart/form-data'
  }
  if (token) {
    config.headers.common = {'Authorization': 'Bearer' + ' ' + token}
  }
  return config
}

let finishFunction = response => {
  return response
}
let errorFunction = error => {
  switch (error.response.status) {
    case 403:
      window.location.href = '/login'
      break
    case 502:
      window.location.href = '/login'
      break
    case 404:
      window.location.href = '/error'
      break
    case 401:
      localStorage.removeItem('admin-token')
      window.location.href = '/login'
      break
    case 302:
      window.location.href = error.response.data
      break
  }
  return Promise.reject(error)
}

const jsonAxios =
  axios.create({ baseURL: Url, timeout: timeOut })
const fileAxios =
  axios.create({ baseURL: Url, timeout: timeOut })

jsonAxios.interceptors.request.use(jsonFunction)
fileAxios.interceptors.request.use(fileFunction)

jsonAxios.interceptors.response.use(finishFunction, errorFunction)
fileAxios.interceptors.response.use(finishFunction, errorFunction)

export function fetch (url, params = {}) {
  return new Promise((resolve, reject) => {
    jsonAxios.get(url, {
      params: params
    }).then(response => {
      resolve(response.data)
    }).catch(err => {
      reject(err)
    })
  })
}

export function post (url, data = {}) {
  return new Promise((resolve, reject) => {
    jsonAxios.post(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
      .catch(error => {
        console.log(error)
      })
  })
}

export function patch (url, data = {}) {
  return new Promise((resolve, reject) => {
    jsonAxios.patch(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}

export function put (url, data = {}) {
  return new Promise((resolve, reject) => {
    jsonAxios.put(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}

export function cascade (url, data = {}) {
  return new Promise((resolve, reject) => {
    jsonAxios.delete(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
  })
}

// 文件上传、下载协议
export function fileFetch (url, params = {}) {
  return new Promise((resolve, reject) => {
    fileAxios.get(url, {
      params: params
    }).then(response => {
      resolve(response.data)
    }).catch(err => {
      reject(err)
    })
  })
}

export function filePost (url, data = {}) {
  return new Promise((resolve, reject) => {
    fileAxios.post(url, data)
      .then(response => {
        resolve(response.data)
      }, err => {
        reject(err)
      })
      .catch(error => {
        console.log(error)
      })
  })
}

export default () => {
  // for use inside Vue files through this.$axios
  // Vue.prototype.$axios = axios
}
